home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / examples / backup < prev    next >
Encoding:
Text File  |  1994-02-08  |  5.7 KB  |  209 lines

  1. #!/usr/local/bin/icmake -qi
  2.  
  3. /* Here are some things you might like to change: */
  4. /* remote tape device and the host where tape device is */
  5. #define TAPEDEV        "/dev/rst0"
  6. #define TAPEHOST    "beatrix"
  7. /* local tape device */
  8. #define PIPE        "/tmp/tape"        
  9. /* username by which tar backup gets sent */
  10. #define USERNAME    "karel"
  11. /* blocksize to be used in remote dd command */
  12. #define BLOCKSIZE    "10k"
  13. /* remote command: rsh for linux/BSD, rcmd for SYSV */
  14. #define REMOTECMD     "rsh"
  15. /* compression: " -z" for gzip, " -Z" for compress, "" for none */
  16. /* note the leading space in the string */
  17. #define COMPRESS " -z"
  18.  
  19. /* Don't change the remainder of this file. */
  20. #define VER         "1.02"
  21. #define TOTAPE        0
  22. #define FROMTAPE    1
  23.  
  24. int
  25.     verbose;                    /* 0: not verbose, 1: ver */
  26.     
  27. list remove_element (list l, int index)        /* remove element #index */
  28. {                        /* from list l */
  29.     int
  30.         i;                    /* counter */
  31.     list
  32.         ret;                    /* returned list */
  33.         
  34.     for (i = 0; i < sizeof (l); i++)        /* loop thru source list */
  35.         if (i != index)                /* add elements to dest */
  36.            ret += (list) element (i, l);    /* except for #index */
  37.            
  38.     return (ret);                /* return dest list */
  39. }
  40.     
  41. void run_system (string sys, int dir)        /* execute tar string, print */
  42. {                        /* string if verbose */
  43.     string
  44.         redir,                    /* local redirection for tar */
  45.         dddir;                    /* remote redirection for dd */
  46.         
  47.     if (dir == TOTAPE)                /* set redir string */
  48.     {
  49.         dddir = "obs=" + BLOCKSIZE + " of";    /* for remote command */
  50.         redir = "<";                /* for local command */
  51.     }
  52.     else
  53.     {
  54.         dddir = "ibs=" + BLOCKSIZE + " if";    /* for remote command */
  55.         redir = ">";
  56.     }
  57.         
  58.     if (verbose)                /* show cmds if verbose */
  59.         printf ("backup: establishing remote drive interface\n");
  60.     
  61.     exec ("su", "-", USERNAME, "-c",         /* run a command as USERNM */
  62.           "\"",    
  63.           REMOTECMD, TAPEHOST,         /* cmd to run is rsh */
  64.               "dd", dddir + "=" + TAPEDEV,     /* rsh should do a dd */
  65.                    "conv=sync",
  66.       "\"", redir, PIPE, "&");        /* cmd should read PIPE */
  67.                         /* and must run in backgr */
  68.                         
  69.     if (verbose)                /* verbose msg */
  70.         printf ("backup: starting ", sys, "\n");
  71.                           
  72.     system (sys);                /* run local tar cmd */
  73. }
  74.  
  75. string makecmd (string opt, list files,     /* make tar system */
  76.         int needverbose)        /* command */
  77. {
  78.     string
  79.         sys;                    /* the system string */
  80.     int 
  81.         i;                    /* loop cntr */
  82.         
  83.     sys = "tar --" + opt;            /* first tar option */
  84.     if (needverbose)
  85.         sys += " --verbose";            /* verbose if requested */
  86.         
  87.     sys += COMPRESS;                /* compression, if needed */
  88.         
  89.     sys += " --file " + PIPE;            /* local tape pipe */
  90.     
  91.     for (i = 0; i < sizeof (files); i++)    /* files to process */
  92.         sys += " " + element (i, files);
  93.         
  94.     return (sys);                /* system string is */
  95. }                        /* returned */
  96.  
  97. void backup (list dirs)                /* writes files/dirs to */
  98. {                        /* tape device */
  99.     string
  100.         sys;                    /* tar cmd */
  101.         
  102.     if (! dirs)                    /* check if argument */
  103.     {                        /* present */
  104.         printf ("backup: store needs directory or file argument(s)\n");
  105.         exit (1);
  106.     }
  107.     
  108.     sys = makecmd ("create", dirs, 1);        /* the system string */
  109.     run_system (sys, TOTAPE);            /* here goes.. */
  110. }
  111.  
  112. void listtape (list files)
  113. {
  114.     string
  115.         sys;                    /* system() string */
  116.         
  117.     sys = makecmd ("list", files, verbose);    /* the system command */
  118.     run_system (sys, FROMTAPE);
  119. }
  120.  
  121. void extract (list files)
  122. {
  123.     string
  124.         sys;
  125.         
  126.     sys = makecmd ("extract", files, verbose);    /* system() string */
  127.     run_system (sys, FROMTAPE);
  128. }
  129.  
  130. void usage ()
  131. {
  132.     printf ("\n"
  133.             "ICCE Tar/Tape Backup Facility  V", VER, "\n"
  134.             "Copyright (c) ICCE 1993. All rights reserved.\n"
  135.             "\n"
  136.             "Backup by Karel Kubat.\n"
  137.             "\n"
  138.             "Usage: backup [-v] operation [arguments]\n"
  139.             "where:\n"
  140.             "    -v flag                     : verbose operation\n"
  141.             "    operation and arguments     : specify what to do, one of:\n"
  142.             "        store file(s) or dir(s) : to backup to tape\n"
  143.             "        list [file(s)]          : to list contents of the tape\n"
  144.             "        restore [file(s)]       : to extract file(s) from tape\n"
  145.             "\n"
  146.             "Currently active defaults:\n"
  147.             "\n"
  148.             "Remote tape device: ", TAPEDEV, "\n"
  149.             "Remote tape host  : ", TAPEHOST, "\n"
  150.             "Used username     : ", USERNAME, "\n"
  151.             "Piping device     : ", PIPE, "\n"
  152.             "\n");
  153. }
  154.  
  155. void prepare_tape ()
  156. {
  157.     if (! exists (PIPE))
  158.         exec ("su", USERNAME, "-c", "\"mknod " + PIPE + " p\"");
  159. }
  160.  
  161. void terminate_tape ()
  162. {
  163.     if (exists (PIPE))
  164.         exec ("su", USERNAME, "-c", "\"rm -f " + PIPE + "\"");
  165. }
  166.  
  167. void main (int argc, list argv, list envp)
  168. {
  169.     string
  170.         selector;                /* option of program */
  171.         
  172.     echo (0);                    /* no cmds re-echoing */
  173.         
  174.     argv = remove_element (argv, 0);        /* remove makefile name */
  175.     selector = element (0, argv);        /* get selector */
  176.     
  177.     if (selector == "-v")            /* if this is the -v flag: */
  178.     {
  179.         verbose++;                /* verbose requested */
  180.         argv = remove_element (argv, 0);    /* remove it */
  181.         selector = element (0, argv);        /* get new selector */
  182.     }
  183.     
  184.     argv = remove_element (argv, 0);        /* remove selector */
  185.  
  186.     if (selector == "store")            /* take action according */
  187.     {                        /* to selector */
  188.         prepare_tape ();
  189.         backup (argv);                
  190.         terminate_tape ();
  191.     }
  192.     else if (selector == "list")
  193.     {
  194.         prepare_tape ();
  195.         listtape (argv);
  196.         terminate_tape ();
  197.     }
  198.     else if (selector == "restore")
  199.     {
  200.         prepare_tape ();
  201.         extract (argv);
  202.         terminate_tape ();
  203.     }
  204.     else
  205.         usage ();
  206.         
  207.     exit (0);                    /* done. */
  208. }
  209.